from flask import request,render_template
from app.lib import MysqlPool
import json
from app.sms import smsbp
import env_config



@smsbp.route('/message', methods=['GET', 'POST'])
def message():
    if request.method =='POST':
        data = {}
        data['data'] = ''
        data['code'] = 0
        data['msg'] = '查询成功'
        env = request.form.get('env_tools')
        if env is None:
            return json.dumps(data)
        phone = request.form.get('phone')
        where=''
        if phone :
            where=" where phone like '%(phone)s'"%{'phone':'%'+phone+'%'}

        dbinfo = env_config.ENV_LIST.get(env.upper()).get('sms_platform')
        dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
        result = dbtool.getAll("select * from %(table_name)s.sms_send_record %(where)s order by create_time desc limit %(page)d,%(pagesize)d;"%{'where':where,'table_name':dbinfo.get('table_name'),'page':int(request.form.get('page'))-1,'pagesize':int(request.form.get('limit'))})
        data['data'] =result
        if result:
            data['count'] = len(result)
        else:
            data['count'] =0

        return json.dumps(data,cls=MysqlPool.DateEncoder)

    return render_template('sms/message.html')